package com.fh.modules.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.dbms.entity.DbmsTableColumn;
import com.fh.modules.dbms.mapper.DbmsTableColumnMapper;
import com.fh.modules.dbms.service.IDbmsTableColumnService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 23/07/2020
 * @Description
 */
@Service
public class DbmsTableColumnServiceImpl extends AbstractServiceImpl<DbmsTableColumnMapper, DbmsTableColumn> implements IDbmsTableColumnService {

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean save(DbmsTableColumn entity) {
        return super.save(entity);
    }
    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean deleteByTableId(Long tableId){
        UpdateWrapper<DbmsTableColumn> updateWrapper=new UpdateWrapper<DbmsTableColumn>();
        updateWrapper.set("update_time",new Date());
        updateWrapper.set("del_flag",1);
        updateWrapper.eq("table_id",tableId);
        updateWrapper.eq("del_flag",0);
        return this.update(updateWrapper);
    }

    public boolean compareTableColumn(Long tableId1,Long tableId2){
        List<DbmsTableColumn> list1=this.queryTableColumns(tableId1);
        List<DbmsTableColumn> list2=this.queryTableColumns(tableId2);
        if(list1.size()==list2.size()){
            int length=list1.size();
            for(int i=0;i<length;i++){
                DbmsTableColumn dbmsTableColumn1=list1.get(i);
                DbmsTableColumn dbmsTableColumn2=list2.get(i);
                if(!dbmsTableColumn1.equals(dbmsTableColumn2)){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public List<DbmsTableColumn> queryTableColumns(Long tableId){
        QueryWrapper<DbmsTableColumn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", tableId).eq("del_flag",0);
        queryWrapper.orderByAsc("column_id");
        return this.list(queryWrapper);
    }

}
